package self.seon.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import self.seon.domain.User;
import self.seon.domain.http.ResponseResult;
import self.seon.service.DoorService;

import javax.servlet.http.HttpServletRequest;


/**
 * @author Seon
 * @date 2022/8/28 21:12
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class DoorController {

    @Autowired
    private DoorService doorService;

    @PostMapping("/login")
    public ResponseResult<?> login(@RequestBody User user, HttpServletRequest request) {
        return doorService.login(request, user);
    }

    @GetMapping("/logout")
    public ResponseResult<?> logout() {
        return doorService.logout();
    }

    /**
     * 在spring容器中将ExpressionRoot注入之后，可以使用spel表达式来调用对应的认证方法
     *
     * @return
     * @ex.hadAuthority('helloWorld')
     */
    @GetMapping("/hello")
    @PreAuthorize("@ex.hadAuthority('helloWorld')")
    public ResponseResult<?> hello() {
        return new ResponseResult<>(1, "你好", "世界");
    }

    @GetMapping("/user")
    @PreAuthorize("hasAnyAuthority('system:user:list')")
    public ResponseResult<?> user() {
        return new ResponseResult<>(1, "南风知我意", "吹梦到西州");
    }

    @GetMapping("/dept")
    @PreAuthorize("hasAnyAuthority('system:dept:list')")
    public ResponseResult<?> dept() {
        return new ResponseResult<>(1, "众生皆苦", "自寻救赎");
    }
}
